import * as fgui from "fairygui-cc";
import AbstractUIWindow from "../../window/AbstractUIWindow";
import UICore from "../../../../Core/Res/UICore";
import { Plane } from "./component/Plane";
import EventMgr from "../../../../Core/Event/EventMgr";
import { LocalUtil } from "../../../../Core/Utils/LocalUtil";
import { ModelGameRobot } from "../../model/ModelGameRobot";
import { ViewManager } from "../../base/ViewManager";
import { Grid } from "./component/Grid";
import TimerMgr from "../../../../Core/Timer/TimerMgr";
import { RandomUtils } from "../../../../Core/Utils/RandomUtils";
import { GameModels } from "../../socket/GameModels";
import WindowObject from "../../window/WindowObject";
import ModuleWindow from "../../window/ModuleWindow";
import SdkTool from "../../../../Core/Sdk/SdkTool";
import AudioManager from "../../../../Core/Audio/AudioManager";
import { MathUtils } from "../../../../Core/Utils/MathUtils";
import { ExternalGLoader } from "../../../../Core/Layer/ExternalGLoader";
import { support } from "../../../proto/protoCmd";

export class RankWindow extends AbstractUIWindow {
    private _selectIndex: number = 1;
    private list: fgui.GList;
    private rank: fgui.GTextField;
    private headUrl: ExternalGLoader;
    private nametxt: fgui.GComponent;
    private eliminateCount: fgui.GTextField;
    private down: fgui.GButton;
    private up: fgui.GButton;
    protected getResList(): Array<string> {
        return [`Rank`];
    }
    protected onInit(): void {
        this._view = UICore.createObject("Rank", "RankWindow").asCom;
    }
    protected onShow(): void {
        EventMgr.on(`event_RespRankInfos`, this.onRespRankInfos, this);
        EventMgr.on(`event_RespRecordData`, this.onRespRecordData, this);
        this._selectIndex = 1;
        this.list.setVirtual();
        this.list.itemRenderer = this.onlist.bind(this);
        this.list.numItems=0
        this.headUrl.url = GameModels.info.playerInfo.headUrl;
        this.nametxt.getChild(`nametxt`).text = GameModels.info.playerInfo.userName;
        this.eliminateCount.text = `击落敌机:${GameModels.info.playerInfo.eliminateCount}`;
        GameModels.support.send_ReqRankInfos(this._selectIndex);
        GameModels.lobby.send_ReqRecordData();
        this.down.onClick(this.clickdown, this);
        this.up.onClick(this.clickup, this);
    }
    private onRespRankInfos(info: support.RespRankInfos): void {
        if (info.SelfRank) {
            this.rank.text = `${info.SelfRank}`;
        }
        if (info.SelfScore) {
            this.eliminateCount.text = `击落敌机:${info.SelfScore}`;
        }
        this.list.data = info.Datas;
        this.list.numItems = info.Datas.length;
        this._selectIndex = info.PageIndex;
    }
    private clickdown(): void {
        GameModels.support.send_ReqRankInfos(this._selectIndex + 1);

    }
    private clickup(): void {
        if (this._selectIndex <= 1) {
            return;
        }
        GameModels.support.send_ReqRankInfos(this._selectIndex - 1);
    }
    private onRespRecordData(): void {
        this.eliminateCount.text = `击落敌机:${GameModels.info.playerInfo.eliminateCount}`;
    }
    private onlist(index: number, item: fgui.GComponent): void {
        let data: support.IRankInfo = this.list.data[index];
        if (data) {
            const rank = item.getChild(`rank`) as fgui.GTextField;
            const headUrl = item.getChild(`headUrl`) as ExternalGLoader;
            const nametxt = item.getChild(`nametxt`).asCom.getChild(`nametxt`) as fgui.GTextField;
            const eliminateCount = item.getChild(`eliminateCount`) as fgui.GTextField;
            rank.text = `${data.Rank}`;
            headUrl.url = data.Avatar ? data.Avatar : UICore.getURL(`Common`, `icon`);
            nametxt.text = data.Nick ? data.Nick : data.Uid;
            eliminateCount.text = `击落敌机:${data.Score}`;
        }
    }
}